package com.lxw.report.meta.utils;


import com.lxw.report.meta.Exception.CommonException;
import com.lxw.report.meta.entity.User;
import com.lxw.report.meta.enums.UserIdentityEnum;
import org.apache.shiro.SecurityUtils;

/**
 * @author lxw
 * @time 2022/3/19 15:48
 */
public class UserUtil {

    /**
     * 获取当前用户实例
     * @return
     */
    public static User getCurrentUser(){
     return (User) SecurityUtils.getSubject().getPrincipal();
    }

    /**
     * 当前用户退出登录
     */
    public static void loginOut(){
        SecurityUtils.getSubject().logout();
    }

    /**
     * 管理员身份校验
     */
    public static void validAdmin() throws CommonException {
        User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
        if(!UserIdentityEnum.isAdmin(currentUser.getUserType())){
            throw new CommonException("权限不足");
        }
    }
}
